package edu.nju.desserthouse.servlets;

import java.io.IOException;
import java.sql.Date;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import nju.edu.desserthouse.model.Bonus;
import nju.edu.desserthouse.model.Goods;
import nju.edu.desserthouse.model.Sale;
import nju.edu.desserthouse.model.Vip;

import edu.nju.desserthouse.dao.BonusDao;
import edu.nju.desserthouse.dao.GoodsDao;
import edu.nju.desserthouse.dao.SaleDao;
import edu.nju.desserthouse.dao.VipDao;

/**
 * Servlet implementation class MakeSaleServlet
 */
@WebServlet("/makesale.clerk")
public class MakeSaleServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
    
	@EJB
	GoodsDao goodsDao;
	@EJB
	VipDao vipDao;
	@EJB
	SaleDao saleDao;
	@EJB
	BonusDao bonusDao;
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MakeSaleServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		execute (request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		execute (request, response);
	}

	public void execute(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
		String vip_account = req.getParameter("vip_account");
		String vip_password = req.getParameter("vip_password");
		Vip vip = vipDao.find("account", vip_account, "password", vip_password);
		long vip_id = vip.getId();
		
		int gID = Integer.valueOf(req.getSession().getAttribute("goods_id").toString());
		int shop_id = Integer.valueOf(req.getSession().getAttribute("clerk_belong").toString());
		int goods_id = shop_id * 30 + gID;
		Goods goods = goodsDao.find("id", Integer.toString(goods_id));
		
		String level = Integer.toString(vip.getLevel());
		Bonus bonus = bonusDao.find("level", level);
		double discount = bonus.getDiscount();
		double price = goods.getPrice();
		
		price *= discount;
		
		if(vip.getBalance() >= price){
			vipDao.buy(Long.toString(vip_id), price);
			
			int goodsNum = goods.getNum();
			goodsNum--;
			goods.setNum(goodsNum);
			goodsDao.updateByGoodsId(goods);
			
			Date now = new Date(new java.util.Date().getTime());
			Sale sale = new Sale();
			sale.setDate(now);
			sale.setGoods(goods_id);
			sale.setIdByDate();
			sale.setPrice(price);
			sale.setShop(gID);
			sale.setVip(vip_id);
			saleDao.save(sale);
			
			req.getSession().setAttribute("actual_price", price);
			req.getSession().setAttribute("balance_left", vip.getBalance());
			req.getSession().setAttribute("vip_level", vip.getLevel());
			req.getSession().setAttribute("discount", discount);
			resp.sendRedirect("/desserthouseWEB/clerk/sellsuccess.jsp");
		}
		
	}
}
